<template>
  <div>
    <el-container>
      <el-main style="margin-left:-12px;font-weight: bolder;">{{id? '编辑广告':'新建广告'}}</el-main>
    </el-container>

    <!-- native指向原生表单  -->
    <el-form label-width="80px" @submit.native.prevent="save">
      <el-form-item label="广告标题">
        <el-input
          prefix-icon="el-icon-edit-outline"
          clearable
          maxlength="20"
          error
          required
          v-model="model.name"
          placeholder="name"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-card class="box-card" style="margin-right: 50px; padding: 10px 50px;">
          <el-row type="flex" style="flex-wrap: wrap; justify-content: space-between">

            <!-- 广告主体 -->
            <el-col :md="11" v-for="(item,i) in model.items" :key="i">

              <el-form-item>
                <el-tag
                  style="text-align: center;border-radius:5px;width: 100%; height: 40px;line-height:40px "
                  type="success"
                >第{{i+1}}个广告位</el-tag>
              </el-form-item>

              <br>
              <el-form-item label-width="80px" label="跳转URL" >
                <el-input  prefix-icon="el-icon-paperclip" v-model="item.url" required></el-input>
              </el-form-item>
              <br>

              <el-form-item label="广告图片">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL+'/upload'"
                  :show-file-list="false"
                  :on-success="response =>$set(item,'image',response.url)"
                >

                  <img v-if="item.image" :src="item.image" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="广告内容">
                <el-input rows="3" v-model="item.title" type="textarea"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button
                  type="danger"
                  style="margin:20px 0;width:100%"
                  @click="model.items.splice(i,1)"
                >删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>

          <el-button style="display: block" id="addskills" @click="model.items.push({})">
            <i class="el-icon-plus"></i>
            添加广告
          </el-button>
        </el-card>
      </el-form-item>

      <el-form-item>
        <el-button class="el-button--medium" type="primary" native-type="submit" :plain="true">保存</el-button>
        <el-button
          type="danger"
          size="medium"
          native-type="button"
          @click="cancel"
          v-show="this.id"
          :plain="true">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data() {
    return {
      model: {
        items: []
      }
    };
  },
  created() {
    this.id && this.fetch();
  },
  methods: {
    async save() {
      //实现新增的保存和编辑的保存
      if (this.id) {
        const response = await this.$http.put(`rest/ads/${this.id}`,this.model);
      } else {
        const response = await this.$http.post("rest/ads", this.model);
      }
      this.$router.push("/ads/list");

      this.$message({
        type: "success",
        message: "保存成功",
        duration: 2000
      });
    },
    async fetch() {
      const response = await this.$http.get(`rest/ads/${this.id}`);
      this.model = Object.assign({}, this.model, response.data);
    },
    //取消保存返回上一页
    cancel() {
      this.$router.go(-1);
    }
  }
};
</script>

<style scoped>
#addAds {
  padding: 15px 39px;
}
.el-button--medium {
  padding: 15px 60px !important;
}
.el-form-item__label {
  text-align: left !important;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
.el-card__body{
  width: 100%;
}
/* 图片框 */
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    min-width: 15rem!important;
    height: 10rem; 
    line-height: 10rem!important;
    text-align: center;
}
.avatar {
    width: 15rem;
    height: auto;
}
</style>